#!/bin/sh

# Note: you need docbook installed, as well as gmake (we need it to
# make private_namespace.h)

# git subtree merge -- not committed at this time.
 git subtree merge -P contrib/jemalloc vendor/jemalloc
cd contrib/jemalloc

# Gut the tests, since they take up too much space.
# Everything else can stay, but if not, add more to trim (there's
# always a trade off between time and saved size.
git rm -rf test msvc
git commit --amend

# kill the tests with empty files so we don't have to hack configure.ac
mkdir -p test/include/test
touch test/include/test/jemalloc_test_defs.h.in
touch test/include/test/jemalloc_test.h.in
echo 'exit 0' > test/test.sh.in

# Reconfigure -- needed only to regenerate the .h files... We don't
# use all the files generated.
#
# Also note: 5.2 lacks --with-lg-page-sizes, but 5.3 has it.
# Also, there's got to be a way to not hard-wire version / hash.
./autogen.sh --enable-xmalloc --enable-fill --enable-lazy-lock --enable-stats \
	     --enable-utrace --with-malloc-conf=abort_conf:false \
	     --with-xslroot=/usr/local/share/xsl/docbook --with-private-namespace=__ \
	     --with-lg-page-sizes=12,13,14,15,16 \
	     --with-version=5.3.0-0-g54eaed1d8b56b1aa528be3bdd1877e59c56fa90c

# Copy over the important generated .h files in configure
cp ./include/jemalloc/jemalloc.h ../../lib/libc/stdlib/malloc/jemalloc/include/jemalloc
git add ../../lib/libc/stdlib/malloc/jemalloc/include/jemalloc/jemalloc.h
cp ./include/jemalloc/jemalloc_defs.h ../../lib/libc/stdlib/malloc/jemalloc/include/jemalloc
git add ../../lib/libc/stdlib/malloc/jemalloc/include/jemalloc/jemalloc_defs.h

# need to make the namespace .h files, and copy a small subset into the tree
# These are super-awkward to generate at buildworld time. Also, we assume we
# only have to make one of these (currently true due to current unlikely to
# change dependencies.
gmake include/jemalloc/internal/private_namespace.h
for i in private_namespace.h jemalloc_internal_defs.h public_namespace.h jemalloc_preamble.h; do
    cp include/jemalloc/internal/$i ../../lib/libc/stdlib/malloc/jemalloc/include/jemalloc/internal/
    git add ../../lib/libc/stdlib/malloc/jemalloc/include/jemalloc/internal/$i
    rm include/jemalloc/internal/$i
done
# OK, commit all the generated files
git add VERSION
git commit --amend

# Clean up the mess
git clean -f .

# Save the cheat sheet
cp ~/jemalloc-upd FREEBSD-upgrade
git add FREEBSD-upgrade
git commit --amend

# Remove hash.c from lib/libc/stdlib/malloc/jemalloc/Makefile.inc
# mutex_pool.c prng.c
# Add
# bin_info.c san.c san_bump.c counter.c prof_data.c prof_log.c prof_recent.c prof_stats.c prof_sys.c
# emap.c edata.c edata_cache.c pa.c pa_extra.c pac.c decay.c hpa.c hpa_hooks.c fxp.c hpdata.c pai.c
# ecache.c ehooks.c eset.c sec.c cache_bin.c peak_event.c psset.c inspect.c exp_grow.c thread_event.c
#

# Manually comment out the following in lib/libc/stdlib/malloc/jemalloc/include/jemalloc/jemalloc.h
# /* #define JEMALLOC_HAVE_ATTR_FORMAT_GNU_PRINTF */
# Add
# #define JEMALLOC_OVERRIDE_VALLOC
# and #include "jemalloc_FreeBSD.h"
# With some adjustments to the old jemalloc_FreeBSD.h, but git can help

# Had to manually remove
# -#define __malloc_options_1_0 JEMALLOC_N(__malloc_options_1_0)
# -#define _malloc_first_thread JEMALLOC_N(_malloc_first_thread)
# -#define __malloc_message_1_0 JEMALLOC_N(__malloc_message_1_0)
# -#define isthreaded JEMALLOC_N(isthreaded)
#
# Also had to remove the following to fix jemalloc 3 ABI compat
# -#define je_allocm JEMALLOC_N(je_allocm)
# -#define je_dallocm JEMALLOC_N(je_dallocm)
# -#define je_nallocm JEMALLOC_N(je_nallocm)
# -#define je_rallocm JEMALLOC_N(je_rallocm)
# -#define je_sallocm JEMALLOC_N(je_sallocm)
# Without the diff you end up with non-exported _je_je*allocm symbols. With you get symbols of the form:
#  365: 000000000018e2a0   406 FUNC    WEAK   DEFAULT   14 rallocm@FBSD_1.3 (5)
#  657: 000000000018e2a0   406 FUNC    GLOBAL DEFAULT   14 __rallocm@FBSD_1.3 (5)
#
